package com.flying.carclub.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.flying.carclub.R;
import com.flying.carclub.activity.AboutActivity;
import com.flying.carclub.activity.FeedbackActivity;
import com.flying.carclub.activity.LoginActivity;
import com.flying.carclub.activity.MyInfoActivity;
import com.flying.carclub.activity.SettingActivity;
import com.flying.carclub.bean.UserInfoBean;
import com.flying.carclub.utils.SPUtils;
import com.stonesun.newssdk.NewsAgent;
import com.stonesun.newssdk.fragment.NewsAFragment;

import static android.app.Activity.RESULT_OK;
import static com.stonesun.newssdk.NewsAgent.getDefaultRecomFragment;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link MineFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MineFragment extends Fragment implements View.OnClickListener {
    private static int REQUEST_CODE_FOR_SETTING_ACTIVITY = 0x0001;
    private static int REQUEST_CODE_FOR_LOGIN_ACTIVITY = 0x0010;
    private static int REQUEST_CODE_FOR_MY_INFO_ACTIVITY = 0x0011;
    private View loginBtn;
    private View headInfoLayout;
    private SimpleDraweeView headerIv;
    private TextView nickNameTv;

    public MineFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment MineFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        initView(view);
        return view;
    }

    private void initView(View contentView) {
        loginBtn = contentView.findViewById(R.id.tv_login);
        headInfoLayout = contentView.findViewById(R.id.layout_head_info);
        headerIv = contentView.findViewById(R.id.iv_header);
        nickNameTv = contentView.findViewById(R.id.tv_nickname);


        contentView.findViewById(R.id.bar_collection).setOnClickListener(this);
        contentView.findViewById(R.id.bar_feedback).setOnClickListener(this);
        contentView.findViewById(R.id.bar_about).setOnClickListener(this);
        contentView.findViewById(R.id.bar_setting).setOnClickListener(this);
        contentView.findViewById(R.id.tv_edit).setOnClickListener(this);
        loginBtn.setOnClickListener(this);
        refreshView();
    }

    private void refreshView() {
        UserInfoBean userInfoBean = SPUtils.getUserInfo(getActivity());

        if (userInfoBean != null) {
            loginBtn.setVisibility(View.GONE);
            headInfoLayout.setVisibility(View.VISIBLE);
            nickNameTv.setText(userInfoBean.username);
        } else {
            loginBtn.setVisibility(View.VISIBLE);
            headInfoLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_edit:
                startActivityForResult(new Intent(getActivity(), MyInfoActivity.class), REQUEST_CODE_FOR_MY_INFO_ACTIVITY);
                break;
            case R.id.tv_login:
                startActivityForResult(new Intent(getActivity(), LoginActivity.class), REQUEST_CODE_FOR_LOGIN_ACTIVITY);
                break;
            case R.id.bar_collection:
                startActivity(new Intent(getActivity(), NewsAgent.getCollectViewActivity()));
                break;
            case R.id.bar_feedback:
                startActivity(new Intent(getActivity(), FeedbackActivity.class));
                break;
            case R.id.bar_about:
                startActivity(new Intent(getActivity(), AboutActivity.class));
                break;
            case R.id.bar_setting:
                startActivityForResult(new Intent(getActivity(), SettingActivity.class), REQUEST_CODE_FOR_SETTING_ACTIVITY);
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_FOR_LOGIN_ACTIVITY || requestCode == REQUEST_CODE_FOR_SETTING_ACTIVITY) {
                refreshView();
            }
        }
        if (requestCode == REQUEST_CODE_FOR_MY_INFO_ACTIVITY) {
            refreshView();
        }
    }
}
